package com.hospital.workstation.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.hospital.common.annotation.RepeatSubmit;
import com.hospital.common.annotation.Log;
import com.hospital.common.core.controller.BaseController;
import com.hospital.common.core.domain.PageQuery;
import com.hospital.common.core.domain.R;
import com.hospital.common.core.validate.AddGroup;
import com.hospital.common.core.validate.EditGroup;
import com.hospital.common.core.validate.QueryGroup;
import com.hospital.common.enums.BusinessType;
import com.hospital.common.utils.poi.ExcelUtil;
import com.hospital.workstation.domain.vo.PharmaceuticalCareVo;
import com.hospital.workstation.domain.bo.PharmaceuticalCareBo;
import com.hospital.workstation.service.IPharmaceuticalCareService;
import com.hospital.common.core.page.TableDataInfo;

/**
 * 药学监护记录
 *
 * @author whb
 * @date 2023-02-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workstation/pharmaceuticalCare")
public class PharmaceuticalCareController extends BaseController {

    private final IPharmaceuticalCareService iPharmaceuticalCareService;

    /**
     * 获取药学监护记录详细信息
     *
     * @param medicalRecordNo 病历号
     */
    @SaCheckPermission("workstation:pharmaceuticalCare:query")
    @GetMapping("/{medicalRecordNo}")
    public R<PharmaceuticalCareVo> getInfo(@NotNull(message = "病历号不能为空")
                                           @PathVariable Integer medicalRecordNo) {
        return R.ok(iPharmaceuticalCareService.queryByMedicalRecordNo(medicalRecordNo));
    }

    /**
     * 新增药学监护记录
     */
    @SaCheckPermission("workstation:pharmaceuticalCare:add")
    @Log(title = "药学监护记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PharmaceuticalCareBo bo) {
        try {
            return iPharmaceuticalCareService.insertByBo(bo);
        } catch (Exception e) {
            return R.fail("保存失败");
        }
    }

    /**
     * 根据ID删除监护记录
     *
     * @param id 药学监护详情id
     * @return
     */
    @Log(title = "删除药学监护详情", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return iPharmaceuticalCareService.removeById(id);
    }

    /**
     * 下载出院用药教育
     *
     * @param response
     * @param medicalRecordNo 病历号
     */
    @SaCheckPermission("workstation:pharmaceuticalCare:download")
    @GetMapping("/download/{medicalRecordNo}")
    public void download(HttpServletResponse response, @PathVariable Integer medicalRecordNo) {
        iPharmaceuticalCareService.exportWord(response, medicalRecordNo);
    }

    /**
     * 下载药学监护记录
     *
     * @param response
     * @param medicalRecordNo 药历号
     */
    @SaCheckPermission("workstation:pharmaceuticalCare:export")
    @GetMapping("/export/{medicalRecordNo}")
    public void exportPharmaceuticalCareRecord(HttpServletResponse response, @PathVariable Integer medicalRecordNo) {
        iPharmaceuticalCareService.exportPharmaceuticalCareRecord(response, medicalRecordNo);
    }
}
